Skip to content

fix(spv): compute required proof headers from actual header difficulties#4039

Open
lionakhnazarov wants to merge 1 commit into
threshold-network:mainfrom
lionakhnazarov:fix/spv-diff1-proof-headers
Open

fix(spv): compute required proof headers from actual header difficulties#4039
lionakhnazarov wants to merge 1 commit into
threshold-network:mainfrom
lionakhnazarov:fix/spv-diff1-proof-headers

Conversation

@lionakhnazarov

Copy link
Copy Markdown
Collaborator

fix(spv): compute required proof headers from actual header difficulties

Problem

The SPV maintainer computed the number of block headers for a proof assuming every header carries the relay epoch difficulty: with txProofDifficultyFactor = 1 it always assembled a single-header proof. On testnet4 (BIP94), sweep transactions are regularly mined in minimum-difficulty (DIFF1) blocks, so the proof contained only a DIFF1 header. The Bridge skips DIFF1 headers when binding to the relay difficulty, finds no decisive header, and reverts with:

execution reverted: Not at current or previous difficulty

Because the error is deterministic, the maintainer crash-looped on the same transaction every cycle (error while maintaining SPV: ... restarting maintainer), and the affected wallet's main UTXO could never be registered on the host chain — blocking all subsequent sweeps for that wallet.

Fix

getProofInfo now mirrors the Bridge's BitcoinTx.determineRequestedDifficulty / evaluateProofDifficulty logic instead of assuming a fixed per-header difficulty:

  • walk headers forward from the proof start block,
  • skip leading minimum-difficulty headers, but only when both relay epoch difficulties are above minimum (so test/dev relays at difficulty 1 keep working),
  • bind the requested difficulty to the first decisive header matching the relay's current or previous epoch difficulty; if it matches neither, skip the transaction (the Bridge would revert),
  • accumulate headers (DIFF1 headers still contribute their work) until total observed difficulty reaches requestedDifficulty × txProofDifficultyFactor,
  • bound the walk by maxProofHeaders = 144 and report "wait for more confirmations" when the chain tip is reached first.

The old epoch-boundary block-count arithmetic is removed; epoch spans are handled implicitly by summing actual header difficulties.

Testing

  • TestGetProofInfo rewritten for the new semantics: current/previous-epoch proofs, epoch-spanning proofs with difficulty drop/raise, leading DIFF1 headers, relay at minimum difficulty, decisive-header mismatch, DIFF1 run exceeding the header bound, and chain tip reached early. Headers are generated with real compact-bits encoding (blockHeaderWithDifficulty).
  • go test ./pkg/maintainer/spv passes.
  • Verified live on the Sepolia/testnet4 stack: a sweep stuck for 10 days (876510fd...62c8, mined in a DIFF1 block at height 137542) was proven on the first cycle after deploying this fix — submitDepositSweepProof tx 0xfeeefad32c7116727949483edfb1e38f8117d923521fd3467886a05226b82577, status 1 — and the maintainer completed its round cleanly.

Notes

  • Requires a Bridge with DIFF1-aware determineRequestedDifficulty (deployed via the DIFF1 library upgrade); against an older Bridge the maintainer now skips such transactions instead of crash-looping.

getProofInfo assumed every proof header carries the relay epoch difficulty,
so with txProofDifficultyFactor=1 it assembled single-header proofs. On
testnet4 (BIP94), sweeps mined in minimum-difficulty blocks produced proofs
containing only a DIFF1 header, which the Bridge rejects with "Not at
current or previous difficulty".

Mirror the Bridge's BitcoinTx logic instead: skip leading DIFF1 headers
when both relay epochs are above minimum, bind the requested difficulty to
the first decisive header matching the relay's current or previous epoch
difficulty, and accumulate headers until their total observed difficulty
covers requestedDifficulty * txProofDifficultyFactor.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@lionakhnazarov, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 17 minutes and 10 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c5ab636e-a35d-4bae-b50d-5cc7ed4baba3

📥 Commits

Reviewing files that changed from the base of the PR and between 4137b34 and 832c116.

📒 Files selected for processing (3)
  • pkg/maintainer/spv/bitcoin_chain_test.go
  • pkg/maintainer/spv/spv.go
  • pkg/maintainer/spv/spv_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant